#!/usr/bin/python
# -*-coding:utf-8-*-
import os
import pandas as pd

try:
    try:
        from zg_factor_analysis_module.base.FactorAnalysisLib import FactorAnalysisLib
        from zg_factor_analysis_module.base.dir_info import factor_lib_dir,basic_data_dir,advance_dir, rank_ic_save_dir, quantile_save_dir, cyb_advance_dir, zbc_factor_data_dir
    except:
        from .base.FactorAnalysisLib import FactorAnalysisLib
        from .base.dir_info import factor_lib_dir, basic_data_dir, advance_dir, rank_ic_save_dir, quantile_save_dir, cyb_advance_dir, zbc_factor_data_dir
except:
    from base.FactorAnalysisLib import FactorAnalysisLib

    from base.dir_info import factor_lib_dir, basic_data_dir, advance_dir, rank_ic_save_dir, quantile_save_dir, cyb_advance_dir, zbc_factor_data_dir



class FactorAnalysisLibDerivative(FactorAnalysisLib):
    def __init__(self,
                 factor_lib_dir=factor_lib_dir,
                 db='public',
                 basic_data_dir=basic_data_dir,
                 rank_ic_save_dir=rank_ic_save_dir,
                 quantile_save_dir=quantile_save_dir,
                 zbc_factor_data_dir=zbc_factor_data_dir,
                 style_lib_dir=cyb_advance_dir,
                 advance_dir=advance_dir):
        # super(FactorAnalysisLib_derivative, self).__init__()
        self.db = db
        self.factor_lib_dir = factor_lib_dir
        self.rank_ic_save_dir=rank_ic_save_dir
        self.quantile_save_dir=quantile_save_dir
        self.basic_data_dir=basic_data_dir
        self.advance_dir=advance_dir
        self.zbc_factor_data_dir=zbc_factor_data_dir
        self.style_lib_dir=style_lib_dir
        #self.df = pd.DataFrame()

    def read_zbc_factor_table(self, filename, filter_list=None, columns=[], start_row=None, end_row=None):
        if len(columns) == 0:
            df = pd.read_hdf(os.path.join(self.zbc_factor_data_dir, '%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             where=filter_list)
        else:
            df = pd.read_hdf(os.path.join(self.factor_lib_dir, '%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             columns=columns,
                             where=filter_list)
        return df

    def read_advance_table(self, filename, filter_list=None, columns=[], start_row=None, end_row=None):
        if len(columns) == 0:
            df = pd.read_hdf(os.path.join(self.advance_dir,'%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             where=filter_list)
        else:
            df = pd.read_hdf(os.path.join(self.advance_dir, '%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             columns=columns,
                             where=filter_list)
        return df

    def read_style_table(self, filename, filter_list=None, columns=[], start_row=None, end_row=None):
        if len(columns) == 0:
            df = pd.read_hdf(os.path.join(self.style_lib_dir, '%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             where=filter_list)
        else:
            df = pd.read_hdf(os.path.join(self.style_lib_dir, '%s.h5' % (filename)),
                             key=filename,
                             mode='r',
                             start=start_row,
                             end=end_row,
                             columns=columns,
                             where=filter_list)
        return df

    # def create_style_table(self, filename, main_columns=None):
    #     self.df.to_hdf(os.path.join(self.style_lib_dir, '%s.h5' % (filename)),
    #                    key=filename,
    #                    format='table',
    #                    data_columns=main_columns,
    #                    mode='w')

    def show_quantile_save_library_db(self):
        '''显示输出文件夹的文件信息'''
        return list(os.walk(self.quantile_save_dir))[0][-1]

    def show_rank_ic_save_library_db(self):
        '''显示输出文件夹的文件信息'''
        return list(os.walk(self.rank_ic_save_dir))[0][-1]

    def show_advance_library_db(self):
        '''显示源数据的文件夹内的文件信息'''
        return list(os.walk(self.advance_dir))[0][-1]

    def show_zbc_factor_library_db(self):
        '''显示源数据的文件夹内的文件信息'''
        return list(os.walk(self.zbc_factor_data_dir))[0][-1]

if __name__ == '__main__':
    F=FactorAnalysisLibDerivative()

    #显示文件夹文件列表
    print(F.show_zbc_factor_library_db())